package com.neo.common.utils;

import java.util.Random;
import java.util.UUID;

/**
 * 产生UUID的唯一性ID的工具类
 *
 * @author winner
 */
public class UUIDGen {
    public static String genUUID() {
        // TODO 随机生成UUID
        UUID uuid = UUID.randomUUID();
        String str = uuid.toString();
        String temp = str.substring(0, 8) + str.substring(9, 13) + str.substring(14, 18) + str.substring(19, 23) + str.substring(24);
        return temp;
    }

    /**
     * 获取uuid，去除-
     *
     * @return
     */
    public static String getUUID() {
        return UUID.randomUUID().toString().replace("-", "");
    }

    //随机生成四位数字
    public static String getCode() {
        int m = (int) (Math.random() * 9000 + 1000);
        return String.valueOf(m);
    }

    //随机生成六位数字
    public static String getRandom() {
        int i = (int) (Math.random() * 900000 + 100000);
        return String.valueOf(i);
    }

    public static String getEightRandom() {
        int i = (int) (Math.random() * 90000000 + 10000000);
        return String.valueOf(i);
    }

    /**
     * 随机生成3位数字
     *
     * @return
     */
    public static String getThreeRandom() {
        int m = (int) (Math.random() * 900 + 100);
        return String.valueOf(m);
    }


    /**
     * 生成充值卡唯一码
     *
     * @param topstr 固定头部，如："601"
     * @param branch 批次号,如："3"
     * @param index  索引号（一个批次号对应一组增序数字，必须唯一）最大长都12位
     * @return 范例返回结果：60114745151779343619
     */
    public static String getOnlyId(String topstr, String branch, Integer index) {
        return topstr + "" + (System.currentTimeMillis() - index * (1000)) + branch + getThreeRandom();
    }

    //随机不重复的6-8位
    public static int card(){
        int[] array = {0,1,2,3,4,5,6,7,8,9};
        Random rand = new Random();
        for (int i = 10; i > 1; i--) {
            int index = rand.nextInt(i);
            int tmp = array[index];
            array[index] = array[i - 1];
            array[i - 1] = tmp;
        }
        int result = 0;
        for(int i = 0; i < 6; i++){
            result = result * 10 + array[i];
        }
        return result;
    }


    //生成随机数字和字母,
    public static String getStringRandom(int length) {

        String val = "";
        Random random = new Random();
        //length为几位密码
        for(int i = 0; i < length; i++) {
            String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
            //输出字母还是数字
            if( "char".equalsIgnoreCase(charOrNum) ) {
                //输出是大写字母还是小写字母
                int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;
                val += (char)(random.nextInt(26) + temp);
            } else if( "num".equalsIgnoreCase(charOrNum) ) {
                val += String.valueOf(random.nextInt(10));
            }
        }
        return val;
    }


    public static void main(String[] args) {
//        String uuid = getUUID();
        System.out.println(getStringRandom(8));

//		int i=(int)(Math.random()*900000+100000); 
//		String messageCode = String.valueOf(i);
//		System.out.println(messageCode);

//		UUID uuid = UUID.randomUUID();
//		String str = uuid.toString();
//		System.out.println(str.replace("-", ""));

//		System.out.println("d53f6012ce9e43179002fbbf842e5dd7".length());
//		System.out.println("okld53f6012ce9e4".length());
//		System.out.println("9002fbbf842e5dd7".length());
        //d53f6012ce9e43179002fbbf842e5dd7
//		appid= "okld53f6012ce9e4";
//		AppKey="9002fbbf842e5dd7";

//		String str = "-456456k45634.98393434234";
//	    System.out.println(RegexUtils.checkDecimals(str));
//	    Boolean strResult = str.matches("-?[0-9]+.*[0-9]*");
//		System.out.println(getOnlyId());
    }
}

